# 
# Created with PyCharm.
# Description :
# User: jiume
# Date: 2024-10-17
# Time: 15:41
#

import random

# d = {1:'Hello',2:'World',3:'Python'}
# print(d) # {1: 'Hello', 2: 'World', 3: 'Python'}
#
# # 增加元素，直接可以赋值
# d[4] = 'Java'
# print(d) # {1: 'Hello', 2: 'World', 3: 'Python', 4: 'Java'}
#
# # 获取所有的key
# keys = d.keys()
# print(keys) # dict_keys([1, 2, 3, 4])
# print(list(keys)) # [1, 2, 3, 4]
# print(tuple(keys)) # (1, 2, 3, 4)
#
# # 获取所有的value
# values = d.values()
# print(values) # dict_values(['Hello', 'World', 'Python', 'Java'])
# print(list(values)) # ['Hello', 'World', 'Python', 'Java']
# print(tuple(values)) # ('Hello', 'World', 'Python', 'Java')
#
# # 将字典的元素转为 key-value的形式，以元组的形式进行展现
# z = d.items()
# print(z) # dict_items([(1, 'Hello'), (2, 'World'), (3, 'Python'), (4, 'Java')])
# print(list(z)) # [(1, 'Hello'), (2, 'World'), (3, 'Python'), (4, 'Java')]
# print(tuple(z)) # ((1, 'Hello'), (2, 'World'), (3, 'Python'), (4, 'Java'))
# print(dict(z)) # {1: 'Hello', 2: 'World', 3: 'Python', 4: 'Java'}
#
# print(d.pop(1)) # Hello
# print(d) # {2: 'World', 3: 'Python', 4: 'Java'}
# print(d.pop(5,"没找到")) # 如果不设置默认值，那么找不到就会报错
#
# print(d.popitem()) # (4, 'Java')-->这是随机删除的
# print(d) # {2: 'World', 3: 'Python'}
#
# d.clear()
# print(d) # {}

# # 第一种方式：
# # 0-2作为键、1-100之间的数作为值
# d = {item:random.randint(1,100) for item in range(3)}
# print(d) # {0: 22, 1: 65, 2: 52} -> 随机的值
#
# # 第二种方式：
# lst1 = [1,2,3]
# lst2 = ['张三','李四','王五']
# d = {key:value for key,value in zip(lst1,lst2)}
# print(d) # {1: '张三', 2: '李四', 3: '王五'}

# # 1、使用{}创建集合
# s = {1,'Hello',('Python',5)}
# print(s) # {1, ('Python', 5), 'Hello'}
#
# # 2、使用内置函数set创建集合
# s = set([1,2,3,4]) # 列表是属于序列，即可迭代的对象
# print(s) # {1, 2, 3, 4}
#
# s = set('Hello Python')
# print(s) # {' ', 'P', 't', 'e', 'n', 'H', 'l', 'h', 'y', 'o'}
#
# # 创建空集合
# print({}, type({})) # {} <class 'dict'>
# print(set(), type(set())) # set() <class 'set'>

# A = {1,2,3,4,5}
# B = {1,3,5,7,9}
# # 交集
# print(A&B) # {1, 3, 5}
# # 并集
# print(A|B) # {1, 2, 3, 4, 5, 7, 9}
# # 差集
# print(A-B) # {2, 4}
# # 补集
# print(A^B) # {2, 4, 7, 9}

# s = {1,2,3,4,5}
# s.add(8)
# print(s) # {1, 2, 3, 4, 5, 8}
# s.remove(3)
# print(s) # {1, 2, 4, 5, 8}
# s.clear()
# print(s) # set()

# # 1、通过for循环遍历
# s = {1,2,3,4,5}
# for item in s:
#     print(item,end=' ')
# print()
# # 2、使用enumerate函数遍历
# for index,value in enumerate(s):
#     print(index,'-->',value)
# # 0 --> 1
# # 1 --> 2
# # 2 --> 3
# # 3 --> 4
# # 4 --> 5

# import random
# # 集合生成式
# s = {random.randint(1,100) for i in range(1,5)}
# print(s) # {34, 2, 82, 7} --> 随机输出的